﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public enum BASECOLOR_SEGMENTATION_MODE
    {
        /// <summary>
        /// 
        /// </summary>
        DEFAULT,
        DEFAULT2,
        BASECOLOR
    }

    /// <summary>
    /// Gets the base color of a given Color.
    /// e.g. x,y,z less than x z less than y == Yellow
    /// </summary>
    public class BaseColorSegmentation : BaseImageFilter
    {
        protected BASECOLOR_SEGMENTATION_MODE M_MODE = BASECOLOR_SEGMENTATION_MODE.DEFAULT;
        protected int m_nBlackThreshold = 10;
        protected int m_nWhiteThreshold = 200;
        protected int m_nGrayThreshold = 10;

        //only used for basemethod2 (default2)
        protected int m_nHalfRGB = Definitions.HalfRGB;


        /// <summary>
        /// Default constructor
        /// </summary>
        public BaseColorSegmentation()
            : this(10,200)
        {
        
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseColorSegmentation"/> class.
        /// </summary>
        /// <param name="_mode">The _mode.</param>
        public BaseColorSegmentation(BASECOLOR_SEGMENTATION_MODE _mode)
            : this(10,200)
        {
            M_MODE = _mode;
        }

        /// <summary>
        /// Constructor to set white and black threshold
        /// </summary>
        /// <param name="_nWhiteThreshold"></param>
        /// <param name="_nBlackThreshold"></param>
        public BaseColorSegmentation(int _nWhiteThreshold, int _nBlackThreshold)
        {
            m_nWhiteThreshold = _nWhiteThreshold;
            m_nBlackThreshold = _nBlackThreshold;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseColorSegmentation"/> class.
        /// </summary>
        /// <param name="_baseColorSegmentation">The _base color segmentation.</param>
        internal BaseColorSegmentation(BaseColorSegmentation _baseColorSegmentation)
        {
            this.m_nWhiteThreshold = _baseColorSegmentation.m_nWhiteThreshold;
            this.m_nBlackThreshold = _baseColorSegmentation.m_nBlackThreshold;
            this.m_nGrayThreshold = _baseColorSegmentation.m_nGrayThreshold;
            this.M_MODE = _baseColorSegmentation.M_MODE;
        }

        /// <summary>
        /// Gets or sets the mode
        /// </summary>
        public BASECOLOR_SEGMENTATION_MODE SegmentationMode
        {
            get { return M_MODE; }
            set { M_MODE = value; }
        }

        /// <summary>
        /// Sets or gets white threshold
        /// </summary>
        public int WhiteThreshold
        {
            get { return m_nWhiteThreshold; }
            set { m_nWhiteThreshold = value; }
        }

        /// <summary>
        /// Sets or gets black threshold
        /// </summary>
        public int BlackThreshold
        {
            get { return m_nBlackThreshold; }
            set { m_nBlackThreshold = value; }
        }

        /// <summary>
        /// Execute this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            switch (M_MODE)
            {
                case BASECOLOR_SEGMENTATION_MODE.DEFAULT:
                    bitmap = BaseMethod(_bitmap);
                    break;

                case BASECOLOR_SEGMENTATION_MODE.DEFAULT2:
                    bitmap = BaseMethod2(_bitmap);
                    break;

                case BASECOLOR_SEGMENTATION_MODE.BASECOLOR:
                    bitmap = BaseColor(_bitmap);
                    break;
            }
            return bitmap;
        }

        /// <summary>
        /// Bases the method.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <returns></returns>
        private UnsafeBitmap BaseMethod(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            Color colorToSet = Color.Transparent;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            Color color = Color.Transparent;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    if (color.R > color.B && color.G > color.B) //Yellow
                    {
                        colorToSet = Color.Yellow;
                    }
                    else if (color.G > color.R && color.B > color.R) //Turquoise
                    {
                        colorToSet = Color.Turquoise;
                    }
                    else if (color.R > color.B && color.B > color.G) //Purple
                    {
                        colorToSet = Color.Purple;
                    }
                    else if (color.R > color.G && color.R > color.B) //Red
                    {
                        colorToSet = Color.Red;
                    }
                    else if (color.R < m_nBlackThreshold && color.G < m_nBlackThreshold && color.B < m_nBlackThreshold) //Black
                    {
                        colorToSet = Color.Black;
                    }
                    else if (color.R > m_nWhiteThreshold && color.G > m_nWhiteThreshold && color.B > m_nWhiteThreshold) //White
                    {
                        colorToSet = Color.White;
                    }
                    else if (color.B > color.G && color.B > color.R) //Blue
                    {
                        colorToSet = Color.Blue;
                    }
                    else if (color.G > color.R && color.G > color.B) // Green
                    {
                        colorToSet = Color.Green;
                    }
                    else if (color.R == color.G && color.G == color.B)
                    {
                        colorToSet = Color.Gray;
                    }
                    bitmap.SetPixel(x, y, colorToSet);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Bases the color.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <returns></returns>
        private UnsafeBitmap BaseColor(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            Color colorToSet = Color.Transparent;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            Color color = Color.Transparent;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    if (color.R > color.B && color.G > color.B) //Yellow
                    {
                        colorToSet = Color.FromArgb(color.R, color.G, 0);
                    }
                    else if (color.G > color.R && color.B > color.R) //Turquoise
                    {
                        colorToSet = Color.FromArgb(0, color.G, color.B);
                    }
                    else if (color.R > color.B && color.B > color.G) //Purple
                    {
                        colorToSet = Color.FromArgb(color.R, 0, color.B);
                    }
                    else if (color.R > color.G && color.R > color.B) //Red
                    {
                        colorToSet = Color.FromArgb(color.R, 0, 0);
                    }
                    else if (color.R < m_nBlackThreshold && color.G < m_nBlackThreshold && color.B < m_nBlackThreshold) //Black
                    {
                        colorToSet = Color.Black;
                    }
                    else if (color.R > m_nWhiteThreshold && color.G > m_nWhiteThreshold && color.B > m_nWhiteThreshold) //White
                    {
                        colorToSet = Color.White;
                    }
                    else if (color.B > color.G && color.B > color.R) //Blue
                    {
                        colorToSet = Color.FromArgb(0, 0, color.B);
                    }
                    else if (color.G > color.R && color.G > color.B) // Green
                    {
                        colorToSet = Color.FromArgb(0, color.G, 0);
                    }
                    else if (color.R == color.G && color.G == color.B)
                    {
                        colorToSet = Color.Gray;
                    }
                    bitmap.SetPixel(x, y, colorToSet);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Bases the method2.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <returns></returns>
        private UnsafeBitmap BaseMethod2(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int nColor = -1;

            Color color = Color.Transparent;

            int y = 0;
            int x = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    nColor = ColorFunctions.RGBMax(color, true);
                    if (nColor != -1)
                    {
                        if ((nColor & 0xFF0000) > 0)
                        {
                            bitmap.SetPixel(x, y, Color.Red);
                        }
                        else if ((nColor & 0x00FF00) > 0)
                        {
                            bitmap.SetPixel(x, y, Color.Green);
                        }
                        else if ((nColor & 0x0000FF) > 0)
                        {
                            bitmap.SetPixel(x, y, Color.Blue);
                        }
                    }
                    else
                    {
                        int nCount = 0;
                        if (color.R == color.G && color.G == color.B)
                        {

                            if (color.R < m_nHalfRGB)
                            {
                                bitmap.SetPixel(x, y, Color.Black);
                                nCount++;
                            }
                            else
                            {
                                bitmap.SetPixel(x, y, Color.White);
                                nCount++;
                            }
                        }
                        if (color.R == color.G && color.B != color.R)
                        {
                            bitmap.SetPixel(x, y, Color.Yellow);
                            nCount++;
                        }
                        if (color.G == color.B && color.R != color.G)
                        {
                            bitmap.SetPixel(x, y, Color.Turquoise);
                            nCount++;
                        }
                        if (color.R == color.B && color.G != color.R)
                        {
                            bitmap.SetPixel(x, y, Color.Purple);
                            nCount++;
                        }
                        if (nCount > 1)
                        {
                            MessageBox.Show("Fehler");
                        }
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Create a copy of this filter
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new BaseColorSegmentation(this);
        }
    }
}
